home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / crlf.zip / CRLF.C next >
C/C++ Source or Header  |  1988-05-12  |  1KB  |  70 lines

  1. /* crlf.c: convert text file between <CR><LF> format and <LF> format. */
  2. /* Intended use: on MS-DOS systems to convert from/to Unix format */
  3. /* Written by Steve Creps, 5-12-88, all rights reserved */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <fcntl.h>
  8. #include <io.h>
  9. #include <ctype.h>
  10.  
  11. static unsigned optr = 0;
  12.  
  13. main(argc, argv)
  14. int argc;
  15. char *argv[];
  16. {
  17.     FILE *fp;
  18.     char c;
  19.     unsigned dash = 0;
  20.  
  21.     while (argc > 1 && argv[1][0] == '-' && !dash) {
  22.         argv++;
  23.         argc--;
  24.         switch(argv[0][1]){
  25.         case 'r':
  26.             optr = 1;
  27.             break;
  28.         case '\0':
  29.             dash = 1;
  30.             break;
  31.         default:
  32.             fprintf(stderr, "crlf: unknown option: %s\n", *argv);
  33.             return(1);
  34.         }
  35.     }
  36.  
  37.     if (argc != 1 && argc != 2) {
  38.         fprintf(stderr, "Usage: crlf [-r -] file\n");
  39.         return(2);
  40.     }
  41.     if (optr) setmode(fileno(stdout), O_BINARY);
  42.     if (argc == 1) {
  43.         if (!optr) setmode(fileno(stdin), O_BINARY);
  44.         catfile(stdin);
  45.     } else if ((fp = fopen(*++argv, "r")) == NULL) {
  46.             fprintf(stderr, "crlf: cannot open %s\n", *argv);
  47.             return(3);
  48.     } else {
  49.             if (!optr) setmode(fileno(fp), O_BINARY);
  50.             catfile(fp);
  51.             fclose(fp);
  52.         }
  53. }
  54.  
  55.  
  56. catfile(fp)
  57. FILE *fp;
  58. {
  59.     char c;
  60.  
  61.     while ((c = getc(fp)) != EOF) {
  62.         while (c != '\n' && c != EOF) {
  63.             putc(c, stdout);
  64.             c = getc(fp);
  65.         }
  66.         putc(c, stdout);
  67.         }
  68.         return;
  69. }
  70.